/**
 * @Description: 导航
 * @Author BPF
 * @create 2021/4/15 9:37
 */
import { Base, Type } from '@bianpengfei/utils'

/**
 * 异步加载路由
 * @param component  异步路由
 * @param componentName  路由名称，缓存路由时用到
 * @returns {Promise<unknown>}
 */
export async function Lazy(component = new Promise(resolve => resolve), componentName = '') {
  const _component = Type.isFunction(component) ? await component() : await component
  console.log('_component', _component)
  //  _component.default?.__file?.slice?.(0, -4)?.replaceAll('/', '-')
  _component.default.name = componentName || Base.UUID().replaceAll('.', '-')
  return Promise.resolve(_component)
}

/**
 * 生成导航信息
 * @param route
 * @returns {{fullPath: *, path, meta, query, name: *, title, params, key: *}}
 */
export const generateNavAttr = (route = {}) => {
  return {
    name: route.name || route.matched.slice(-1)?.shift()?.components?.default?.name,
    path: route.path,
    fullPath: route.fullPath || route.path,
    meta: route.meta,
    title: route.title,
    params: route.params,
    query: route.query,
    key: route.key || route.fullPath
  }
}
